package com.yurun.loginmodule.controller;

import com.yurun.commonapi.api.CommonResult;
import com.yurun.commonapi.pojo.UmsRole;
import com.yurun.loginmodule.util.JWTUtil;
import com.yurun.orgpermissionmodule.service.OrgPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletRequest;
import java.math.BigInteger;

@Api(tags = "角色模块")
@RestController
@RequestMapping("/permissionManage")
public class OrgPermissionController {

    @Autowired
    private OrgPermissionService permissionService;

    @ApiOperation(value = "新增角色")
    @RequiresPermissions("/addRole")
    @RequestMapping("/addRole")
    public CommonResult addRole(ServletRequest request, UmsRole umsRole){ //ServletRequest request, String name, String description, int status, String roleIds
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        umsRole.setOrganizationId(BigInteger.valueOf(Long.parseLong(organizationId)));
        return permissionService.addRole(umsRole);
    }

    @ApiOperation(value = "所有角色信息")
    @RequiresPermissions("/allRoleInfo")
    @RequestMapping("/allRoleInfo")
    public CommonResult allRoleInfo(ServletRequest request, String keyword, int pageNum, int pageSize){
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        return permissionService.allRoleInfo(BigInteger.valueOf(Long.parseLong(organizationId)), keyword, pageNum, pageSize);
    }

    @ApiOperation(value = "某一角色信息")
    @RequiresPermissions("/getOneRoleInfo")
    @RequestMapping("/getOneRoleInfo")
    public CommonResult getOneRoleInfo(int id) {
        return permissionService.getOneRoleInfo(id);
    }

    @ApiOperation(value = "修改角色")
    @RequiresPermissions("/editRole")
    @RequestMapping("/editRole")
    public CommonResult editRole(int id, int[] roleIds, String name, String description, int status) {
        return CommonResult.success(permissionService.editRole(id, roleIds, name, description, status));
    }

    @ApiOperation(value = "更新角色状态")
    @RequiresPermissions("/updateRoleStatus")
    @RequestMapping("/updateRoleStatus")
    public CommonResult updateRoleStatus(int id) {
        return permissionService.updateRoleStatus(id);
    }


    @ApiOperation("获取所有角色名")
    @RequestMapping("/getRoleOption")
    public CommonResult getRoleOption(ServletRequest request) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        return permissionService.getRoleOption(BigInteger.valueOf(Long.parseLong(organizationId)));
    }
}
